#!/usr/bin/env raku
# ---------------------------------------------------------------
# SPDX-License-Identifier: GPL-3.0-or-later
# ---------------------------------------------------------------
# Test File     : 20-regression.rakutest
# Test Authors  : Aoran Zeng <ccmywish@qq.com>
# Created On    : <2025-09-27>
# Last Modified : <2025-09-27>
#
# $(cwd) 为 test/ 时:
#
#     raku ./20-regression.rakutest
# ---------------------------------------------------------------

use lib '../lib';

use Test;

# 少些一点文件名
my $f = Nil;

sub run-rawstr4c($file) {
  # 相当糟糕的是，note 输出到 stderr，merge 的时候会和 stdout 的输出顺序搞乱。。。
  # 所以有时候不是我们在终端上看到的那样
  # my $proc = run 'raku', '-I../lib', '../bin/rawstr4c', "input/$file", :out($stdout_flag), :merge($merge_flag);
  # 因此我们还是分别捕获吧
  my $proc = run 'raku', '-I../lib', '../bin/rawstr4c', "input/$file", :out, :err;
  [$proc.out.slurp.chomp, $proc.err.slurp.chomp];
}

# 这里不能用q，因为q中 \ 在起作用
is run-rawstr4c("")[0], Q:to/EOF/
char _rawstr4c_test_in_raku[] = "say \"hello world\";";
EOF
, "Default file";

# 同时也检查一下lint的信息
my ($result, $lint) = run-rawstr4c("lint.md");

is $result, Q:to/EOF/
char _rawstr4c_section_in_bash[] = "echo Test linting";
EOF
, "lint.md";

# 这里需要 chomp 一下
is $lint, Q:to/EOF/.chomp
[Deprecate] config 'keep-prefix' is deprecated, please use 'no-prefix' instead
[Deprecate] config 'keep-postfix' is deprecated, please use 'no-postfix' instead
[Warning!!] Unrecognized config 'unknown-option' (ignored)
[Warning!!] Unrecognized config 'another-bad-option' (ignored)
EOF
, "Lint";



sub test($file, $expected) {
  is run-rawstr4c($file)[0], $expected, $file;
}

test "translate-escape.md", Q:to/EOF/;
char _rawstr4c_test_for_escape_mode_in_bash[] = "echo \"Hello World!\"\nprintf \"Line with \\\"quotes\\\" and 'apostrophes'\\n\"";
EOF

test "translate-hex.md", Q:to/EOF/;
char _rawstr4c_test_for_hex_mode_in_bash[] = "\x65\x63\x68\x6f\x20\x22\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21\x22";
EOF

test "translate-oct.md", Q:to/EOF/;
char _rawstr4c_test_for_oct_mode_in_bash[] = "\145\143\150\157\040\042\110\145\154\154\157\040\127\157\162\154\144\041\042";
EOF

test "no-trailing-new-line.md", Q:to/EOF/;
char _rawstr4c_in_sh[] = "echo 'hello world'\necho 'hello Raku'\n";
EOF


test "hierarchy-with-root.md", Q:to/EOF/;
char _rawstr4c_in_c[] = "int root_code_block = 1;";

char _rawstr4c_maven_config_in_xml[] = "<settings>\n  <mirrors>\n  </mirrors>\n</settings>";

char _rawstr4c_gradle_config_in_groovy[] = "repositories {\n  maven { url 'https://example.com' }\n}";

char _rawstr4c_pip_config_in_bash[] = "pip config set global.index-url https://example.com";

char _rawstr4c_conda_config_in_yaml[] = "channels:\n  - https://example.com/conda";

char _rawstr4c_dockerfile_in_dockerfile[] = "FROM ubuntu:20.04\nRUN echo \"Hello World\"";

char _rawstr4c_multi_stage_build_in_dockerfile[] = "FROM node:16 AS builder\nWORKDIR /app\nCOPY . .\nRUN npm install";
EOF


test "hierarchy-without-root.md", Q:to/EOF/;
char _rawstr4c_maven_config_in_xml[] = "<settings>\n  <mirrors>\n  </mirrors>\n</settings>";

char _rawstr4c_gradle_config_in_groovy[] = "repositories {\n  maven { url 'https://example.com' }\n}";

char _rawstr4c_pip_config_in_bash[] = "pip config set global.index-url https://example.com";

char _rawstr4c_conda_config_in_yaml[] = "channels:\n  - https://example.com/conda";

char _rawstr4c_dockerfile_in_dockerfile[] = "FROM ubuntu:20.04\nRUN echo \"Hello World\"";

char _rawstr4c_multi_stage_build_in_dockerfile[] = "FROM node:16 AS builder\nWORKDIR /app\nCOPY . .\nRUN npm install";
EOF


test "inherited-config.md", Q:to/EOF/;
char ROOT_in_c[] = "root code;";

char L1_level_1_in_c[] = "level1 code;";

char L1_level_2_in_c[] = "level2 code;";

char L3_level_3_in_c[] = "level3 code;";
EOF



$f = "output-to-default-files.md";
run-rawstr4c($f);
ok "./input/rawstr4c.c".IO.e, "$f Generate .c";
ok "./input/rawstr4c.h".IO.e, "$f Generate .h";

"./input/rawstr4c.c".IO.unlink;
"./input/rawstr4c.h".IO.unlink;

$f = "output-to-terminal-besides-save-files.md";
$result = run-rawstr4c($f)[0];
ok $result.contains(Q:to/EOF/
char _rawstr4c_some_config_in_toml[] = "[setting]\ninit = true";
EOF
), "$f";
ok "./input/rawstr4c.c".IO.e, "$f Generate .c";
ok "./input/rawstr4c.h".IO.e, "$f Generate .h";

$f = "output-to-customized-files.md";
run-rawstr4c($f);
ok "./input/custom_strings_impl.c".IO.e, "$f Generate custom_strings_impl.c";
ok "./input/custom_strings.h".IO.e,      "$f Generate custom_strings.h";

done-testing;
